// 引入文件
import { getSetting, chooseAddress, openSetting, showModal, showToast } from "../../utils/asyncWx.js";
import regeneratorRuntime from "../../lib/runtime/runtime.js";
/*
  1.页面加载的时候
    1.从缓存中获取购物车数据，渲染到页面中
      1.这些数据的 checked = true
*/
Page({
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    // 1.获取缓存中的收货地址信息
    const address = wx.getStorageSync("address");
    // 获取缓存中的数组
    let cart = wx.getStorageSync("cart") || [];
    // 购物车过滤后的数据
    cart = cart.filter(v=>v.checked);
    // 设置data
    this.setData({
      address
    })
    // 调用
    let checkedCart = cart.filter(v=>v.checked);
    
    // 总价格，总数量
    let totalPrice = 0;
    let totalNum = 0;

    cart.forEach(v => {
      totalPrice += v.data.message.goods_price * v.num;
      totalNum += v.num;
    })
    this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    });
  }
})